from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from time import sleep
import os
import datetime
import json


def write_bilibili_video_data(item):
    # print('write_bilibili_video_data: ', item)
    source_dir = 'video_data'
    target_dir = os.path.join(source_dir, item['videoid'])
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    file_name = "{}.txt".format(datetime.datetime.now().strftime('%Y%m%d'))
    file_path = os.path.join(target_dir, file_name)
    with open(file_path, mode='a', encoding='utf8') as file:
        file.write(json.dumps(item, ensure_ascii=False))
        file.write('\n')


def parse_video_url_info(driver, videoid):
    url = "https://www.bilibili.com/video/{}/".format(videoid)
    driver.get(url)
    sleep(3)

    item = {}
    item['title'] = driver.find_element(By.XPATH, './/h1[contains(@class, "video-title")]').get_attribute('title')
    item['link'] = url
    item['videoid'] = videoid
    item['play_time'] = driver.find_element(By.XPATH, './/div[@class="view-text"]').text
    item['comments'] = driver.find_element(By.XPATH, './/div[@class="dm-text"]').text
    item['duration'] = driver.find_element(By.XPATH, './/span[@class="bpx-player-ctrl-time-duration"]').text
    if not driver.find_elements(By.XPATH, './/div[@class="up-detail-top"]/a[1]'):
        item['author'] = driver.find_element(By.XPATH, './/div[@class="staff-info"]/a[1]').text
    else:
        item['author'] = driver.find_element(By.XPATH, './/div[@class="up-detail-top"]/a[1]').text
    item['date'] = driver.find_element(By.XPATH, './/div[@class="pubdate-ip-text"]').text
    item['datetime'] = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')

    print(item)
    sleep(1)
    return item



def read_bilibili_video_data(driver):
    source_dir = 'video_data'
    # for path, dirs, files in os.walk(source_dir):
    #     print(path, dirs, files)
    for dir in os.listdir(source_dir):
        try:
            item = parse_video_url_info(driver, dir)
            write_bilibili_video_data(item)
        except:
            continue



if __name__ == '__main__':
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--window-size=3840x2160")
    prefs = {'profile.managed_default_content_settings.images': 2}
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chrome_options)

    read_bilibili_video_data(driver)

    driver.quit()